<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>开启头部工具栏 - 数据表格</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/back/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/back/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>数据表格</cite></a>
        <a><cite>开启头部工具栏</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">入住信息</div>
                <div class="layui-card-body">
                    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">酒店名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="hotelName" id="hotel_name" placeholder="请输入" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">所在地区</label>
                                <div class="layui-input-block">
                                    <input type="text" name="hotelCity" id="hotel_city" placeholder="请输入" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">地址</label>
                                <div class="layui-input-block">
                                    <input type="text" name="hotelAddress" id="hotel_address" placeholder="请输入" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search" id="fuzzyQuery">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
                            </div>
                        </div>
                    </div>

                    <table class="layui-hide" id="test-table-toolbar" lay-filter="test-table-toolbar"></table>

                    <script type="text/html" id="test-table-toolbar-toolbarDemo">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                            <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
                        </div>
                    </script>

                    <script type="text/html" id="test-table-toolbar-barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="${pageContext.request.contextPath }/back/layui/layui.js"></script>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
    function renderdata(hotel_name,hotel_city,hotel_address){
        layui.config({
            base: '${pageContext.request.contextPath }/back/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table'], function(){
            var admin = layui.admin
                ,$ = layui.$
                ,form = layui.form
                ,table = layui.table;

            table.render({
                elem: '#test-table-toolbar'
                ,url: '${pageContext.request.contextPath }/HotelController/getAllHotel?hotel_name='+hotel_name+'&hotel_city='+hotel_city+'&hotel_address='+hotel_address
                ,toolbar: '#test-table-toolbar-toolbarDemo'
                ,title: '酒店信息'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'hotel_id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                    ,{field:'hotel_name', title:'酒店名', width:100, edit: 'text'}
                    ,{field:'hotel_city', title:'酒店所在地区', width:100, edit: 'text'}
                    ,{field:'hotel_address', title:'酒店位置', width:100, edit: 'text'}
                    ,{field:'hotel_tel', title:'联系电话',width:210}
                    ,{fixed: 'right', title:'操作', toolbar: '#test-table-toolbar-barDemo', width:150}
                ]],
                request: {pageName: 'pageNum',limitName: 'pageSize'}
                ,page: true
                ,limit:5   //默认十条数据一页
                ,limits: [3, 5, 8, 10, 15, 20]
            });

            //头工具栏事件
            table.on('toolbar(test-table-toolbar)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：'+ data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test-table-toolbar)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        //根据id值删除酒店信息
                        var  hotel_id=data.hotel_id;
                        $.ajax({
                            type : "POST",
                            url : "${pageContext.request.contextPath }/HotelController/getUpdateHotel",
                            data : {
                                hotel_id:hotel_id,
                            },
                            dataType : "json",
                            success : function(data) {
                                layer.msg('删除成功');
                                $(".layui-laypage-btn").click() ;
                            }
                        })
                        obj.del();
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    layer.confirm('确认修改', function(index){
                        layer.alert(JSON.stringify(data));
                        //提交 Ajax 成功后，静态更新表格中的数据
                        //修改酒店信息
                        $.ajax({
                            type : "POST",
                            url : "${pageContext.request.contextPath }/HotelController/getModHotel",
                            data : {
                                hotel_id:data.hotel_id,
                                hotel_name:data.hotel_name,
                                hotel_city:data.hotel_city,
                                hotel_address:data.hotel_address,
                            },
                            dataType : "json",
                            success : function(data) {

                                layer.msg('修改成功');
                                $(".layui-laypage-btn").click() ;
                            }
                        })

                        layer.close(index);
                    });
                }
            });
            //事件
            var active = {
                add: function(){
                    layer.open({
                        type: 2
                        ,title: '添加入住信息'
                        ,content: '${pageContext.request.contextPath }/BackController/hotelform'
                        ,area: ['450px', '600px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-back-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据

                                table.reload('LAY-user-front-submit'); //数据刷新
                                layer.close(index); //关闭弹层
                            });
                            submit.trigger('click');
                        }
                    })
                }
            }
            $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    }
    $(function() {
        //渲染数据
        renderdata("","","");

        //模糊查询
        $("#fuzzyQuery").click(function(){
            var hotel_name=$("#hotel_name").val();
            var hotel_city=$("#hotel_city").val();
            var hotel_address=$("#hotel_address").val();
            renderdata(hotel_name,hotel_city,hotel_address);
        })
    })
</script>
</body>
</html>